package product;

import java.util.ArrayList;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;



public class Airport {

	/**统计出山东省没有飞机场的城市有哪些
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		if (args.length!=3) {
			System.err.println("path err");
			System.exit(-1);
		}
		Job job=new Job(new Configuration(),"no");
		job.setJarByClass(Airport.class);
		
		MultipleInputs.addInputPath(job, new Path(args[0]), TextInputFormat.class,amaps.class);
		MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class, apmap.class);
		
		FileOutputFormat.setOutputPath(job, new Path(args[2]));
		job.setReducerClass(areduce.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		job.waitForCompletion(true);
	}
	public static class amaps extends Mapper<LongWritable, Text,Text,Text>{
		public static final String LABEL="c_";
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			String city = value.toString();
			context.write(new Text(city),new Text(LABEL+city));
		};
	}
	public static class apmap extends Mapper<LongWritable, Text,Text,Text>{
		public static final String LABEL="a_";
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			String[] lines = value.toString().split("\t");
			context.write(new Text(lines[0]),new Text(LABEL+lines[1]));
		};
	}
	public static class areduce extends Reducer<Text,Text,Text,Text>{
		protected void reduce(Text k2, java.lang.Iterable<Text> values, org.apache.hadoop.mapreduce.Reducer<Text,Text,Text,Text>.Context context) throws java.io.IOException ,InterruptedException {
			ArrayList<String> arrayList=new ArrayList<>();
			String air=null;
			for (Text str : values) {
				String info = str.toString();
				if (info.startsWith(amaps.LABEL)) {
					arrayList.add(info.substring(2));
				}else if (info.startsWith(apmap.LABEL)) {
					air=info.substring(2);
				}
			}
			if (air==null && arrayList.size()>0) {
				for (String cityair : arrayList) {
					context.write(new Text(cityair),new Text());
				}
			}
		};
	}

}
